/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.core.config;

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author 挺好的 2023年06月07日 11:22
 */
@Configuration
@EnableAsync
public class AsyncConfig {

    /**
     * 线程池
     */
    @Resource (name = "threadPoolProperties")
    private ThreadPoolProperties threadPoolProperties;

    @Bean
    public Executor taskExecutor () {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(this.threadPoolProperties.getCoreSize());
        executor.setMaxPoolSize(this.threadPoolProperties.getMaxSize());
        executor.setQueueCapacity(this.threadPoolProperties.getQueueCapacity());
        executor.setThreadNamePrefix(this.threadPoolProperties.getThreadPoolNamePrefix());
        //设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
